/*
 * xAPBSCtable.java
 *
 * Java class used to store data of multiple xAP BSC endpoints.
 * A dynamically sizable vector is used so that new endpoints can be added
 * anytime.
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 13/08/09 by Daniel Berenguer : first version
 */

package xAP;


import java.util.Vector;

/********************************************************************************************
 * xAPBSCtable class
 *
 * Class used to store xAP data
 *
 *********************************************************************************************/
public class xAPBSCtable
{
   /********************************************************************************************
    * Private member variables
    ********************************************************************************************/
    // Vector of endpoints
    private Vector vTable;

   /********************************************************************************************
    * xAPBSCtable
    *
    * Class constructor
    *
    ********************************************************************************************/
    public xAPBSCtable()
    {
        // Create vector of endpoints
        vTable = new Vector();
    }

   /********************************************************************************************
    * updateTableFromMessage
    *
    * Update endpoint in the vector or add new endpoint or device if it doesn't exists from the
    * xAP BSC message passed as argument
    *
    * 'message'    Message to extract data from
    *
    * Returns:
    *   true if the endpoint has changed
    *   false if the endpoint has not changed
    ********************************************************************************************/
    public boolean updateTableFromMessage(xAPBSCmessage message)
    {
        xAPBSCendpoint endpoint = new xAPBSCendpoint(message);

        return updateEndpoint(endpoint);
    }

   /********************************************************************************************
    * updateEndpoint
    *
    * Update endpoint in the vector or add new endpoint if it doesn't exists
    *
    * 'endpoint'    Endpoint to update
    *
    * Returns:
    *   true if the endpoint has changed
    *   false if the endpoint has not changed
    ********************************************************************************************/
    public boolean updateEndpoint(xAPBSCendpoint endpoint)
    {
        // Index of endpoint in table
        int intInd = indexOfEndpointFromUID(endpoint.uid);

        // New endpoint?
        if (intInd == -1)
        {
            // Add endpoint to the table
            addEndpoint(endpoint);
        }
        else    // The endpoint already exists in the vector
        {
            xAPBSCendpoint epElement = (xAPBSCendpoint)vTable.elementAt(intInd);

            // Update endpoint in table only if it's changed
            if (epElement.equals(endpoint) == false)
            {
                vTable.setElementAt(endpoint, intInd);
                return true;
            }
        }

        return false;
    }

   /********************************************************************************************
    * getEndpointFromSource
    *
    * Return the endpoint containing the source address passed as argument
    *
    * 'Source'    xAP source address
    *
    * Returns:
    *   endpoint
    ********************************************************************************************/
    public xAPBSCendpoint getEndpointFromSource(String Source)
    {
        xAPBSCendpoint epElement;
        int i;

        for(i=0 ; i<vTable.size() ; i++)
        {
            epElement = (xAPBSCendpoint)vTable.elementAt(i);

            if (epElement.source.compareToIgnoreCase(Source) == 0)
                return epElement;
        }

        return null;
    }

   /********************************************************************************************
    * indexOfEndpointFromUID
    *
    * Search the index of the endpoint given its xAP UID
    *
    * 'UID'    UID to search
    *
    * Returns:
    *   index into the vector
    *   -1 if the endpoint is not found
    ********************************************************************************************/
    private int indexOfEndpointFromUID(String UID)
    {
        xAPBSCendpoint epElement;
        int i;

        for(i=0 ; i<vTable.size() ; i++)
        {
            epElement = (xAPBSCendpoint)vTable.elementAt(i);

            if (epElement.uid.compareToIgnoreCase(UID) == 0)
                return i;
        }

        return -1;
    }

   /********************************************************************************************
    * getUIDfromSource
    *
    * Search the xAP UID of an endpoint given its Source adddress
    *
    * 'Source'    Source address to search
    *
    * Returns:
    *   UID string
    ********************************************************************************************/
    public String getUIDfromSource(String Source)
    {
        xAPBSCendpoint epElement;
        int i;

        for(i=0 ; i<vTable.size() ; i++)
        {
            epElement = (xAPBSCendpoint)vTable.elementAt(i);

            if (epElement.source.compareToIgnoreCase(Source) == 0)
                return epElement.uid;
        }

        return null;
    }

   /********************************************************************************************
    * length
    *
    * Length of the table
    *
    * Returns:
    *   table length
    ********************************************************************************************/
    public int length()
    {
        return vTable.size();
    }

   /********************************************************************************************
    * length
    *
    * Length of the table
    *
    * Returns:
    *   table length
    ********************************************************************************************/
    public xAPBSCendpoint endpointAt(int index)
    {
        return (xAPBSCendpoint)vTable.elementAt(index);
    }

   /********************************************************************************************
    * addEndpoint
    *
    * Add new endpoint at the end of the vector
    *
    * 'endpoint'    Endpoint to add
    *
    ********************************************************************************************/
    private void addEndpoint(xAPBSCendpoint endpoint)
    {
        vTable.addElement(endpoint);
    }

   /********************************************************************************************
    * isOutput
    *
    * Return true if the BSC endpoint is an output
    *
    * 'source'      Source address
    *
    *  Return:
    *   true if the BSC endpoint is an output
    *   false if it's an input
    ********************************************************************************************/
    public boolean isOutput(String source)
    {
        xAPBSCendpoint epElement;
        int i;

        for(i=0 ; i<vTable.size() ; i++)
        {
            epElement = (xAPBSCendpoint)vTable.elementAt(i);

            if (epElement.source.compareToIgnoreCase(source) == 0)
            {
                if (epElement.isOutput())
                    return true;
                else
                    return false;
            }
        }

        return false;
    }
}
